// pages/add/add.js
const app = getApp();
Page({
  data: {
    chosed: true,  //步骤class
    start_time: '',
    end_time: '',
    btn_show: false, //第一步第二步按钮显示隐藏
    ac_box: false,
    ac_name: '',
    ac_num: '',
    no_step: 1,
    goodsList: [],
    goodId: null,
    searchKey: "",
    selectedGoodsList: [], //选择的商品
    checkAllGoods: false,  //保存选择SKU是否全选
    SKUList: [],
    msgText: '没有相关商品',
    pageTotal: 0, //分页总数
    p: 1
  },
  // 时间
  bindDateChange1: function (e) {
    this.setData({
      start_time: e.detail.value
    })
  },
  bindDateChange2: function (e) {
    this.setData({
      end_time: e.detail.value
    })
  },
  // 活动名称
  ac_name: function (e) {
    this.setData({
      ac_name: e.detail.value
    })
  },
  // 参团人数
  ac_num: function (e) {
    this.setData({
      ac_num: e.detail.value
    })
  },
  // 基本信息->商品信息
  next: function () {
    var that = this;
    var ac_name = that.data.ac_name;
    var start_time = that.data.start_time;
    var end_time = that.data.end_time;
    var ac_num = that.data.ac_num;
    if (ac_name != '' && start_time != '' && end_time != '' && ac_num != '') {
      if (new Date(start_time).getTime() > new Date(end_time).getTime()) {
        app.showToast('结束时间需大于开始时间');
        return;
      }
      if (ac_num >= 2 && ac_num <= 10) {
        that.setData({
          btn_show: true,
          chosed: false,
          ac_box: true
        })
        wx.showLoading({
          title: '加载中...',
        })
        wx.request({
          url: app.buildUrl('/shop/active/add-select'),
          method: "GET",
          header: app.buildHeader(),
          data: {
            p: that.data.p
          },
          success(res) {
            wx.hideLoading();
            if (res.data.code == 200) {
              let data = res.data.data;
              that.setData({
                goodsList: data.lists,
                pageTotal: data.count.total_page
              })
            } else {
              app.showToast(res.data.msg);
            }
          }
        })
      } else {
        app.showToast('请输入2~10人');
      }
    } else {
      app.showToast('请完善信息');
    }
  },
  // 商品信息->基本信息
  prev: function () {
    this.setData({
      btn_show: false,
      chosed: true,
      ac_box: false
    })
  },
  // 搜索商品输入
  inp_cont(e) {
    let that = this;
    that.setData({
      searchKey: e.detail.value
    })
    if (e.detail.value == "") {
      that.setData({
        msgText: '没有相关商品'
      })
      wx.showLoading({
        title: '重新获取列表...',
      })
      wx.request({
        url: app.buildUrl('/shop/active/add-select'),
        method: "GET",
        header: app.buildHeader(),
        data: {
          p: 1,
          goods_name: ""
        },
        success(res) {
          wx.hideLoading();
          if (res.data.code == 200) {
            that.setData({
              goodsList: res.data.data.lists,
              pageTotal: res.data.data.count.total_page
            })
          } else {
            app.showToast(res.data.msg);
          }
        }
      })
    }
  },
  // 搜索
  search() {
    let that = this;
    let key = that.data.searchKey;
    if (key == "" || !key) {
      app.showToast('请输入搜索内容');
    } else {
      wx.showLoading({
        title: '加载中...',
      })
      wx.request({
        url: app.buildUrl('/shop/active/add-select'),
        method: "GET",
        header: app.buildHeader(),
        data: {
          p: 1,
          goods_name: key
        },
        success(res) {
          wx.hideLoading();
          if (res.data.code == 200) {
            if (res.data.data.lists.length == 0) {
              that.setData({
                msgText: '没有搜索到相关内容'
              })
            }
            that.setData({
              goodsList: res.data.data.lists,
              pageTotal: res.data.data.count.total_page
            })
          } else {
            app.showToast(res.data.msg);
          }
        }
      })
    }
  },
  toggleList(e) {
    let that = this;
    let key = that.data.searchKey;
    that.setData({
      p: e.target.dataset.p
    })
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: app.buildUrl('/shop/active/add-select'),
      method: "GET",
      header: app.buildHeader(),
      data: {
        p: that.data.p,
        goods_name: key
      },
      success(res) {
        wx.hideLoading();
        if (res.data.code == 200) {
          that.setData({
            goodsList: res.data.data.lists,
            pageTotal: res.data.data.count.total_page
          })
        } else {
          app.showToast(res.data.msg);
        }
      }
    })
  },
  // 选择商品
  choose_commodity: function (e) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    that.setData({
      no_step: 2,
      goodId: e.target.dataset.goodid,
      checkAllGoods: false,
      selectedGoodsList: []
    })
    wx.request({
      url: app.buildUrl('/shop/active/getsku'),
      method: "POST",
      header: app.buildHeader(),
      data: {
        id: that.data.goodId
      },
      success(res) {
        wx.hideLoading();
        let list = res.data.data.goods_attr;
        if (res.data.code == 200) {
          list.tbody.forEach(i => {
            i.check = false
          })
          that.setData({
            selectedGoodsList: list
          })
        } else {
          app.showToast(res.data.msg)
        }
      }
    })
  },
  // step2上一步
  step2_prev: function () {
    this.setData({
      no_step: 1
    })
  },
  // step2下一步
  step2_next: function () {
    let that = this;
    let checkStatus = false;
    that.data.selectedGoodsList.tbody.forEach(i => {
      if (i.check) {
        checkStatus = true
      }
    })
    if (checkStatus) {
      this.setData({
        no_step: 3
      })
      that.setData({
        SKUList: that.data.selectedGoodsList.tbody.filter(item => item.check == true)
      })
    } else {
      app.showToast("请选择SKU");
    }
  },
  // 选择SKU
  checkGoods(e) {
    let that = this;
    let checkedId = e.target.dataset.checkId;
    let newList = that.data.selectedGoodsList;
    let num = 0;
    newList.tbody.forEach(i => {
      if (checkedId == i.id) {
        i.check = !i.check
      }
      if (i.check) {
        num++
      }
    })
    if (num == newList.tbody.length) {
      that.setData({
        checkAllGoods: true
      })
    } else if (num < newList.tbody.length) {
      that.setData({
        checkAllGoods: false
      })
    }
    that.setData({
      selectedGoodsList: newList
    })
  },
  //选择全部
  checkAll(e) {
    let that = this;
    let list = that.data.selectedGoodsList;
    let status = that.data.checkAllGoods;
    that.setData({
      checkAllGoods: !that.data.checkAllGoods
    })
    if (status) {
      list.tbody.forEach(i => {
        i.check = false;
      })
    } else {
      list.tbody.forEach(i => {
        i.check = true;
      })
    }
    that.setData({
      selectedGoodsList: list
    })
  },
  // step3上一步
  step3_prev: function () {
    this.setData({
      no_step: 2
    })
  },
  // step3确认
  step3_confirm: function () {
    let that = this;
    let dataObj = that.data;
    let submitArr = [];
    wx.showLoading({
      title: '正在添加...',
    })
    dataObj.SKUList.forEach(i => {
      submitArr.push({
        
        "attr_img": i.images,
        // "one_price": i.one_price || i.original_price,
        "original_price": i.original_price,
        "collage_price": i.discount_price,
        "inventory": i.inventory,
        "goods_attre_id": i.attr_id,
        "attribute_value_id": i.attribute_value_id
      })
    })
    // console.log(submitArr)
    wx.request({
      url: app.buildUrl('/shop/active/add'),
      header: app.buildHeader(),
      method: "POST",
      data: {
        "active_attr": JSON.stringify(submitArr),
        "active_name": dataObj.ac_name,
        "active_num": dataObj.ac_num,
        "begin_time": dataObj.start_time,
        "end_time": dataObj.end_time,
        "goods_id": dataObj.goodId
      },
      success(res) {
        wx.hideLoading();
        if (res.data.code == 200) {
          app.showToast(res.data.msg)
          setTimeout(() => {
            // wx.reLaunch({
            //   url: '../manage/manage'
            // })
            wx.navigateBack({
              delta: 1
            })
          }, 1000)
        } else {
          app.showToast(res.data.msg)
        }
      }
    })
  },
  original_price(e) { //修改原价
    this.setSkuList(e)
  },
  one_price(e) {//修改单购价
    this.setSkuList(e)
  },
  discount_price(e) { //修改拼团价
    this.setSkuList(e)
  },
  inventory(e) { //修改库存
    this.setSkuList(e)
  },
  setSkuList(e) { //修改SKU列表对应项
    let that = this;
    let setName = e.target.dataset.skuName;
    let list = that.data.SKUList;
    list.forEach(i => {
      if (i.id == e.target.dataset.skuId) {
        i[setName] = e.detail.value
      }
    })
    that.setData({
      SKUList: list
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  }
})